package utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeConvert {
	//Timestamp格式日期时间转为字符串
	public static String stampToStr(Timestamp ts){
		String timeStr = "";
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			timeStr = dateFormat.format(ts);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return timeStr;
	}
	
	public static Long getNowTimeLong(){
		return (new Date()).getTime();
	}
	
	public static Long getTimeSpace(Long time)
	{
		return (new Date()).getTime()-time;
	}
	
	
	public static Timestamp strToStamp(String timeStr){
		Timestamp timestamp = null;
		try {
			timestamp = Timestamp.valueOf(timeStr);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return timestamp;
	}
	
	public static String stampToStr(Timestamp ts, String timeFormat){
		String timeStr = "";
		DateFormat dateFormat = new SimpleDateFormat(timeFormat);
		try {
			timeStr = dateFormat.format(ts);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return timeStr;
	}
	
	public static Timestamp strToStamp(String strTime, String timeFormat){

		Timestamp timestamp = null;
		Date date = new Date(); 
		DateFormat sdf = new SimpleDateFormat(timeFormat); 
		try{
			date = sdf.parse(strTime); 
			timestamp = new Timestamp(date.getTime());

		}catch(Exception e){
			
		}
		return timestamp;
	}
	
	//获取当前日期
	public static String getNowDate(){
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH)+1;
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int week = calendar.get(Calendar.DAY_OF_WEEK);
		String nowDate = year + "年" + month + "月" +  day + "日 星期"; 
		switch (week) {
		case 1:
			nowDate = nowDate + "日";
			break;
		case 2:
			nowDate = nowDate + "一";
			break;
		case 3:
			nowDate = nowDate + "二";
			break;
		case 4:
			nowDate = nowDate + "三";
			break;
		case 5:
			nowDate = nowDate + "四";
			break;
		case 6:
			nowDate = nowDate + "五";
			break;
		case 7:
			nowDate = nowDate + "六";
			break;
		default:
			break;
		}
		return nowDate;
	}
	
	//获取当前时间
	public static String getNowTime(){
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH)+1;
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		int second = calendar.get(Calendar.SECOND);
		String nowTime = year + "年" + month + "月" +  day + "日" + hour + "点" + minute + "分" + second + "秒";
		return nowTime;
	}
}
